package file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

public class FileUtil {

	public static void writeLines(String file, ArrayList<String> lines) {

		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(
					new File(file), true));
			for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
				String string = (String) iterator.next();
				bw.write(string);
				bw.newLine();
				bw.newLine();
			}
			bw.close();
		} catch (Exception e) {
		}
	}

	public static ArrayList<String> getLines(String file) {
		ArrayList<String> lines = new ArrayList<String>();
		try {
			// Open the file that is the first
			// command line parameter
			FileInputStream fstream = new FileInputStream(file);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				// Print the content on the console
				lines.add(strLine);
			}
			// Close the input stream
			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		return lines;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String file = "C:/Users/moazzamkhan/Desktop/3000words.txt";
		String file1 = "C:/Users/moazzamkhan/Desktop/words2";
		ArrayList<String> lines = getLines(file);
//		for (int i = 0; i < lines.size(); i++) {
//			String line = lines.get(i);
//			System.out.println(line);
//		}
		writeLines(file1, lines);
	}

}
